package cc.laop.java.concurrent;

import java.util.concurrent.Exchanger;

/**
 * @Auther: Pengpeng
 * @Date: create in 2018/1/11 17:55
 * @Description:
 */
public class ExchangerTest {

    private static Exchanger<String> changer = new Exchanger<>();

    public static void threadA() {
        new Thread(() -> {
            try {
                String msg = changer.exchange("I am A!");
                System.out.println("threadA: " + msg);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public static void threadB() {
        new Thread(() -> {
            try {
                String msg = changer.exchange("I am B!");
                System.out.println("threadB: " + msg);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public static void main(String[] args) throws InterruptedException {
        threadA();
        threadB();
        Thread.sleep(2000);
    }

}
